package com.ymatou.iapi.m.couponservice.testcase;

import java.util.Date;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.m.couponservice.parameter.CreateCouponBean;
import com.ymatou.iapi.m.couponservice.parameter.InvalidBatchBean;
import com.ymatou.iapi.m.couponservice.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.m.couponservice.service.CreateCouponCall;
import com.ymatou.iapi.m.couponservice.service.InvalidBatchCall;
import com.ymatou.iapi.m.couponservice.service.SellerCreateBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceMCallService;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;

import oracle.net.aso.n;
/**
*作废优惠券批次信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_InvalidBatch{
	private static InvalidBatchBean invalidbatchBean;
	private static InvalidBatchCall invalidbatchCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("作废优惠券批次信息");
	}

	@Before
	public void caseUp() {
		invalidbatchBean=new InvalidBatchBean();
		invalidbatchCall=new InvalidBatchCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(invalidbatchCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_InvalidBatch_001() {
		Logger.start(true,"作废优惠券批次信息-作废商家批次");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(sellerCreateBatchCall.getBatchCode()).get(0);
			
			invalidbatchBean.setSellerId(batch.getiOperatorId());
			invalidbatchBean.setBatchCode(batch.getsBatchCode());
			
			invalidbatchCall.setData(invalidbatchBean);
			invalidbatchCall.callService();			
			
			Logger.verifyEquals("0", invalidbatchCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", invalidbatchCall.getMessage(), "验证返回Message");
			verifyDB();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_InvalidBatch_002() {
		Logger.start(true,"作废优惠券批次信息-作废平台领取批次");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponServiceMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall = couponServiceMCallService.createPlatformCoupon(createCouponBean);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			
			invalidbatchBean.setSellerId(0);
			invalidbatchBean.setInvalidUser("2345");
			invalidbatchBean.setInvalidMemo("作废评优领取优惠券");
			invalidbatchBean.setBatchCode(batch.getsBatchCode());
			
			invalidbatchCall.setData(invalidbatchBean);
			invalidbatchCall.callService();			
			
			Logger.verifyEquals("0", invalidbatchCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", invalidbatchCall.getMessage(), "验证返回Message");
			verifyDB();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_InvalidBatch_003() {
		Logger.start(true,"作废优惠券批次信息-作废平台兑换批次");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponServiceMCallService.setDefaultPlatformCreateCouponBeanForConversion();
			CreateCouponCall createCouponCall = couponServiceMCallService.createPlatformCoupon(createCouponBean);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			
			invalidbatchBean.setSellerId(0);
			invalidbatchBean.setInvalidUser("2345");
			invalidbatchBean.setInvalidMemo("作废评优领取优惠券");
			invalidbatchBean.setBatchCode(batch.getsBatchCode());
			
			invalidbatchCall.setData(invalidbatchBean);
			invalidbatchCall.callService();			
			
			Logger.verifyEquals("0", invalidbatchCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", invalidbatchCall.getMessage(), "验证返回Message");
			verifyDB();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_InvalidBatch_004() {
		Logger.start(true,"作废优惠券批次信息-作废平台批次传商家ID");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponServiceMCallService.setDefaultPlatformCreateCouponBeanForConversion();
			CreateCouponCall createCouponCall = couponServiceMCallService.createPlatformCoupon(createCouponBean);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			
			invalidbatchBean.setSellerId(3384);
			invalidbatchBean.setInvalidUser("2345");
			invalidbatchBean.setInvalidMemo("作废评优领取优惠券");
			invalidbatchBean.setBatchCode(batch.getsBatchCode());
			
			invalidbatchCall.setData(invalidbatchBean);
			invalidbatchCall.callService();			
			
			Logger.verifyEquals("2", invalidbatchCall.getCode(), "验证返回code");
			Logger.verifyIsContains("您没有权限作废此批次，请勿非法操作", invalidbatchCall.getMessage(), "验证返回Message");
	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_InvalidBatch_005() {
		Logger.start(true,"作废优惠券批次信息-作废平台批次-InvalidUser空");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(sellerCreateBatchCall.getBatchCode()).get(0);
			
			invalidbatchBean.setSellerId(0);
			invalidbatchBean.setInvalidMemo("作废评优领取优惠券");
			invalidbatchBean.setBatchCode(batch.getsBatchCode());
			
			invalidbatchCall.setData(invalidbatchBean);
			invalidbatchCall.callService();			
			
//			Logger.verifyEquals("2", invalidbatchCall.getCode(), "验证返回code");
			Logger.verifyIsContains("作废人不能为空", invalidbatchCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_InvalidBatch_007() {
		Logger.start(true,"作废优惠券批次信息-作废平台批次-InvalidMemo空");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(sellerCreateBatchCall.getBatchCode()).get(0);
			
			invalidbatchBean.setSellerId(0);
			invalidbatchBean.setInvalidUser("3344");
			invalidbatchBean.setBatchCode(batch.getsBatchCode());
			
			invalidbatchCall.setData(invalidbatchBean);
			invalidbatchCall.callService();			
			
//			Logger.verifyEquals("2", invalidbatchCall.getCode(), "验证返回code");
			Logger.verifyIsContains("作废理由不能为空", invalidbatchCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_InvalidBatch_008() {
		Logger.start(true,"作废优惠券批次信息-作废平台批次-batchId空");
		try {
		
			invalidbatchBean.setSellerId(0);
			invalidbatchBean.setInvalidUser("3344");
			invalidbatchBean.setInvalidMemo("334fddfgdfg4");
			
			invalidbatchCall.setData(invalidbatchBean);
			invalidbatchCall.callService();			
			
//			Logger.verifyEquals("2", invalidbatchCall.getCode(), "验证返回code");
			Logger.verifyIsContains("batchCode:必须传入批次编号", invalidbatchCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_InvalidBatch_006() {
		Logger.start(true,"作废优惠券批次信息-作废平台兑换批次-不传InvalidUser");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponServiceMCallService.setDefaultPlatformCreateCouponBeanForConversion();
			CreateCouponCall createCouponCall = couponServiceMCallService.createPlatformCoupon(createCouponBean);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			
			invalidbatchBean.setSellerId(0);
			invalidbatchBean.setBatchCode(batch.getsBatchCode());
			
			invalidbatchCall.setData(invalidbatchBean);
			invalidbatchCall.callService();			
			
//			Logger.verifyEquals("2", invalidbatchCall.getCode(), "验证返回code");
			Logger.verifyIsContains("作废人不能为空", invalidbatchCall.getMessage(), "验证返回Message");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_InvalidBatch_009() {
		Logger.start(true,"作废优惠券批次信息-作废已作废商家批次");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(sellerCreateBatchCall.getBatchCode()).get(0);
			
			invalidbatchBean.setSellerId(batch.getiOperatorId());
			invalidbatchBean.setBatchCode(batch.getsBatchCode());
			
			invalidbatchCall.setData(invalidbatchBean);
			invalidbatchCall.callService();		
			
			invalidbatchCall.setData(invalidbatchBean);
			invalidbatchCall.callService();	
			
			Logger.verifyEquals("0", invalidbatchCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", invalidbatchCall.getMessage(), "验证返回Message");
			verifyDB();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_InvalidBatch_010() {
		Logger.start(true,"作废优惠券批次信息-作废商家批次-sellerid与batchid不匹配");
		try {
			Map batch  = new YmtCouponBatchWapper().getTop1SellerCouponInfo().get(0);
	
			invalidbatchBean.setSellerId(2222);
			invalidbatchBean.setBatchCode(batch.get("sBatchCode").toString());
			
			invalidbatchCall.setData(invalidbatchBean);
			invalidbatchCall.callService();			
			
//			Logger.verifyEquals("2", invalidbatchCall.getCode(), "验证返回code");
			Logger.verifyIsContains("您没有权限作废此批次，请勿非法操作", invalidbatchCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_InvalidBatch_011() {
		Logger.start(true,"作废优惠券批次信息-作废商家批次-batchid不存在");
		try {
			Map batch  = new YmtCouponBatchWapper().getTop1SellerCouponInfo().get(0);
	
			invalidbatchBean.setSellerId(Integer.parseInt(batch.get("iOperatorId").toString()));
			invalidbatchBean.setBatchCode("747257");
			
			invalidbatchCall.setData(invalidbatchBean);
			invalidbatchCall.callService();			
			
//			Logger.verifyEquals("2", invalidbatchCall.getCode(), "验证返回code");
			Logger.verifyIsContains("批次不存在", invalidbatchCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private void verifyDB() {
		YmtCouponBatch couponBatch = new YmtCouponBatchWapper().selectBysBatchCode(invalidbatchBean.getBatchCode())
				.get(0);

		Logger.verifyEquals(true, couponBatch.getbInvalidStatus(), "验证数据库中YmtCouponBatch表中bInvalidStatus字段正确");
		Logger.verifyEquals(true, checkDateNew(couponBatch.getdInvalidTime()),
				"验证数据库中YmtCouponBatch表中dInvalidTime字段正确");

		if (invalidbatchBean.getInvalidUser() != null) {
			Logger.verifyEquals(invalidbatchBean.getInvalidUser(), couponBatch.getInvalidUser(),
					"验证数据库中YmtCouponBatch表中InvalidUser字段正确");
		}

		if (invalidbatchBean.getInvalidMemo() != null) {
			Logger.verifyEquals(invalidbatchBean.getInvalidMemo(), couponBatch.getInvalidMemo(),
					"验证数据库中YmtCouponBatch表中InvalidMemo字段正确");
		}

	}
	
	 private boolean checkDateNew(Date date)
	    {
	        Date now = new Date();
	        if (date.getYear() != now.getYear() && date.getMonth() != now.getMonth() && date.getDate() != now.getDate()
	                && date.getHours() != now.getHours() && Math.abs(date.getMinutes() - now.getMinutes()) > 3)
	        {
	            return false;
	        }
	        return true;
	    }
}